package com.hllxd.hism.framework.rpc.client.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hllxd.hism.framework.rpc.client.basic.dto.RequestDto;
import com.hllxd.hism.framework.rpc.client.basic.dto.ResponseDto;
import com.hllxd.hism.framework.rpc.client.utils.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xiyong.lxy
 * @date 2020-10-24 18:42
 */

@Slf4j
public class RpcHttpClient {
    String target;
    /** 使用默认值 600s */
    long deadLine = 0L;
    public RpcHttpClient(String target) {
        this.target = target;
    }

    public void afterPropertiesSet() throws Exception {
//        this.keepAliveEnabled = false;
//        GrayConfig.init(this.grayProperties);
//        this.maxMessageSize = GrpcConfig.getRpcClientMaxMessageSize();
//        this.maxMetadataSize = GrpcConfig.getRpcClientMaxMetadataSize();
//        this.grpcClientManager = new GrpcClientManager(this.target, this.maxMessageSize, this.maxMetadataSize, this.keepAliveEnabled, this.loadBalancerFactory, this.interceptors);
    }

    public void withDeadLine(long sec) {
        this.deadLine = sec;
    }

    public ResponseDto request(RequestDto dto) {
        String request = JSON.toJSONString(dto);
        JSONObject response = OkHttpUtil.ajaxPost(request, target + "/cdata/framework/hrpc/request.do", deadLine);
        if(response==null) {
            return null;
        }
        if(!OkHttpUtil.isSuccess(response)) {
            log.warn("result is failed. {}", request);
//            return null;
        }
        return JSON.toJavaObject(response, ResponseDto.class);
    }

}
